package com.chen.controller;

import com.chen.dto.ExamPlanDTO;
import com.chen.service.ClassesService;
import com.chen.service.CourseService;
import com.chen.service.ExamPaperService;
import com.chen.service.ExamPlanService;
import com.chen.vo.ClassesVO;
import com.chen.vo.CourseVO;
import com.chen.vo.ExamPaperVO;
import com.chen.vo.ExamPlanVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 考试计划相关接口
 */
@Controller
@RequestMapping("/examPlan")
public class ExamPlanController {

    @Autowired
    private ExamPlanService examPlanService;

    // 班级信息
    @Autowired
    private ClassesService classesService;

    // 科目
    @Autowired
    private CourseService courseService;

    // 试卷
    @Autowired
    private ExamPaperService examPaperService;

    /**
	 * 查询学生待考信息
	 * @param classId 学生所在班级编号
	 * @param gradeId 学生所在年级编号
	 * @return
	 */
    @GetMapping("/willexams")
    public ModelAndView getStudentWillExam(@RequestParam("classId") Integer classId, @RequestParam("gradeId") Integer gradeId) {
        ModelAndView model = new ModelAndView();
        model.setViewName("/reception/examCenter");
        List<ExamPlanVO> examPlans = examPlanService.getStudentWillExam(classId, gradeId);
        model.addObject("examPlans", examPlans);
        model.addObject("gradeId", gradeId);
        return model;
    }

    /**
     * 待考记录
     * @return
     */
    @GetMapping("/examPlans")
    public ModelAndView getExamPlans() {
        ModelAndView model = new ModelAndView();
        model.setViewName("/admin/examPlans");
        List<ExamPlanVO> examPlans = examPlanService.getExamPlanList();
        model.addObject("examPlans", examPlans);
        return model;
    }

    /**
     * 进入新增待考记录页面
     * @return
     */
    @RequestMapping("/preAddep")
    public ModelAndView preAddep() {
        ModelAndView model = new ModelAndView();
        model.setViewName("/admin/examPlanAdd");
        //获取所有班级信息
        List<ClassesVO> classes = classesService.findAllClasses();
        model.addObject("classes", classes);
        //获取所有科目信息
        List<CourseVO> courses = courseService.findAll();
        model.addObject("courses", courses);
        //获取所有的试卷信息 -- 纯净的
        List<ExamPaperVO> examPapers = examPaperService.findAll();
        model.addObject("examPapers", examPapers);
        return model;
    }

    /**
     * 进入编辑页面
     * @param examPlaneId
     * @return
     */
    @RequestMapping(value="/edit/{examPlaneId}", method= RequestMethod.GET)
    public ModelAndView editExamPlaneId(@PathVariable("examPlaneId") Integer examPlaneId) {
        ModelAndView model = new ModelAndView();
        model.setViewName("/admin/examPlanedit");
        //获取所有班级信息
        List<ClassesVO> classes = classesService.findAllClasses();
        model.addObject("classes", classes);
        //获取所有科目信息
        List<CourseVO> courses = courseService.findAll();
        model.addObject("courses", courses);
        //获取所有的试卷信息
        List<ExamPaperVO> examPapers = examPaperService.findAll();
        model.addObject("examPapers", examPapers);
        ExamPlanVO examPlanVO = examPlanService.getExamPlanById(examPlaneId);
        model.addObject("examPlan", examPlanVO);
        return model;
    }

    /**
     * 删除考试计划
     * @param examPlaneId
     * @return
     */
    @GetMapping("/del/{examPlaneId}")
    public String del(@PathVariable("examPlaneId") Integer examPlaneId) {
        examPlanService.delById(examPlaneId);
        return "redirect:/examPlan/examPlans";
    }

    /**
     * 新增或编辑考试计划信息
     * @param dto
     * @return
     */
    @PostMapping("/saveOrUpdate")
    public String saveOrUpdate(ExamPlanDTO dto) {
        examPlanService.saveOrUpdate(dto);
        return "redirect:/examPlan/examPlans";
    }

    /**
     * 检测过期考试计划
     * 意在于考试时间已经过期了，则自动删除
     * 检查时间每五分钟执行异常
     */
    @Scheduled(cron="0 0/5 * * * ?")
    public void checkExpirePlan() {
        examPlanService.deleteExpirePlan();
    }

}
